<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<head th:include="header">
    <title>部门管理-编辑</title>
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="ibox float-e-margins">
            <div class="ibox-content">
                <form class="form-horizontal" id="fm" data-validator-option="{timely:3, focusCleanup:true}">
                    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
                    <input type="hidden" id="id" name="id" th:value="${enterprise.id}">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">企业名称：</label>
                        <div class="col-sm-7">
                            <input id="enterpriseName" name="enterpriseName" th:value="${enterprise.enterpriseName}" type="text" class="form-control"
                                data-rule="required;length(~50)">
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">负责人姓名：</label>
                        <div class="col-sm-7">
                            <input id="mainPerson" name="mainPerson" th:value="${enterprise.mainPerson}" type="text" class="form-control" data-rule="required;length(~30)">
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">企业负责人移动电话号码：</label>
                        <div class="col-sm-7">
                            <input id="code" name="mainPersonMobile" th:value="${enterprise.mainPersonMobile}" type="text" class="form-control" data-rule="required;length(~12);mobile;">
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">安全负责人姓名：</label>
                        <div class="col-sm-7">
                            <input id="safePerson" name="safePerson" th:value="${enterprise.safePerson}" type="text" class="form-control" data-rule="required;length(~30)">
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">安全负责人移动电话号码：</label>
                        <div class="col-sm-7">
                            <input id="safePersonMobile" name="safePersonMobile" th:value="${enterprise.safePersonMobile}" type="text" class="form-control"
                                data-rule="required;length(~12);mobile;">
                        </div>
                    </div>
                   
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">所属行业:</label>
                        <div class="col-sm-7">
                            <select id="sel_menu1" name="industryCode" class="form-control"></select>
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">所属区域:</label>
                        <div class="col-sm-7">
                            <select id="sel_menu2" name="areaCode" class="form-control"></select>
                        </div>
                    </div>  
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">风险模型类型:</label>
                        <div class="col-sm-7" id="riskModelArea">
                           
                        </div>
                    </div>                 
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <div class="text-center">
                            <button class="btn btn-primary" type="submit">保存</button>
                            <button class="btn btn-white" type="reset">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div th:include="footer"></div>
    <script type="text/javascript" th:inline="javascript">
     var pid=[[${ enterprise.industryCode }]];
     var riskModel = [[${ enterprise.riskModel }]];       
      $(function () {
            var industryCode = [[${ enterprise.industryCode }]];
            var areaCode = [[${ enterprise.areaCode }]];
            var industryCodeArray = new Array();
            industryCodeArray.push(industryCode);
            var areaCodeArray = new Array();
            areaCodeArray.push(areaCode);
            BindSelect("sel_menu1", basePath + "dic/select2Tree/9", industryCodeArray);
            BindSelect("sel_menu2", basePath + "dic/select2Tree/16", areaCodeArray);                
            buildRiskModelSelect();
        });

        $("#fm").validator({
            valid: function (form) {
                var me = this;
                // 提交表单之前，hold住表单，防止重复提交
                me.holdSubmit();
                $.ajax({
                    url: basePath + "enterprise/edit",
                    data: $(form).serialize(),
                    type: "POST",
                    dataType: 'json',
                    success: function (data) {
                        if (data.success) {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.msg(data.msg, {
                                icon: 1,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            }, function () {
                                window.parent.location.reload();
                                parent.layer.close(index);
                            });

                        } else {
                            parent.layer.msg(data.msg, {
                                icon: 2,
                                time: 2000 //2秒关闭（如果不配置，默认是3秒）
                            });
                            me.holdSubmit(false);
                        }
                    }
                });
            }
        });

        /**
     * 绑定字典内容到指定的Select控件
     * ctrlName ID
     * url 后台服务地址
     * arryObj 被选中的ID
     */
        function BindSelect(ctrlName, url, arryObj) {
            var control = $('#' + ctrlName);
            $.ajax({
                url: url,
                type: 'get',
                dataType: 'json',
                cache: true,
                success: function (data) {
                    control.empty();
                    $.each(data, function (i, item) {
                        if (arryObj != null && arryObj.length > 0) {
                            if (contains(arryObj, item.id)) {
                                control.append("<option value='" + item.id + "' selected='selected'>&nbsp;" + item.text + "</option>");
                            } else {
                                control.append("<option value='" + item.id + "'>&nbsp;" + item.text + "</option>");
                            }

                        } else {
                            control.append("<option value='" + item.id + "'>&nbsp;" + item.text + "</option>");
                        }
                    });
                }
            });
            //设置Select2的处理
            control.select2({
                allowClear: true,
                placeholder: "请选择",
                language: "zh-CN",
                escapeMarkup: function (m) { return m; }
            });
        }

        //判断数组中是否存在某值
        function contains(arr, obj) {
            var i = arr.length;
            while (i--) {
                if (arr[i] === obj) {
                    return true;
                }
            }
            return false;
        }        
       
       //构建风险模型下拉框
       function buildRiskModelSelect(){    	  
    	   var content="<select id='riskModel' name='riskModel' multiple='multiple' data-tpggle='tooltip' data-placement='bottom' title='多选' class='form-control' data-rule='required'></select>";
    	   $("#riskModelArea").html(content);
    	   var riskModelArray=riskModel.split(",");
    	   BindSelect("riskModel", basePath + "dic/select2Tree/"+pid,riskModelArray);
       }
       
       /**
        * 绑定字典内容到指定的Select控件
        * ctrlName ID
        * url 后台服务地址
        * arryObj 被选中的ID
        */
       function BindSelect(ctrlName, url, arryObj) {
           var control = $('#' + ctrlName);
           $.ajax({
               url: url,
               type: 'get',
               dataType: 'json',
               cache: true,
               success: function (data) {
                   control.empty();
                   $.each(data, function (i, item) {
                       if (arryObj != null && arryObj.length > 0) {
                           if (contains(arryObj, item.id)) {
                               control.append("<option value='" + item.id + "' selected='selected'>&nbsp;" + item.text + "</option>");
                           } else {
                               control.append("<option value='" + item.id + "'>&nbsp;" + item.text + "</option>");
                           }

                       } else {
                           control.append("<option value='" + item.id + "'>&nbsp;" + item.text + "</option>");
                       }
                   });
               }
           });
           //设置Select2的处理
           control.select2({
               allowClear: true,
               placeholder: "请选择",
               language: "zh-CN",
               escapeMarkup: function (m) {
                   return m;
               }
           });
       }

       //判断数组中是否存在某值
       function contains(arr, obj) {
           var i = arr.length;
           while (i--) {
               if (arr[i] === obj) {
                   return true;
               }
           }
           return false;
       }
       
       //行业下拉框改变事件
       $("#sel_menu1").on("change",function(){
    	   pid=$("#sel_menu1").val();
           riskModel ="";            
           buildRiskModelSelect();
       })
       
    </script>
</body>

</html>